import { AuthAPI } from '../index'
import { post, get } from '@/utils/request'

export interface LoginParams {
  username: string
  password: string
  remember?: boolean
}

export interface RegisterParams {
  username: string
  password: string
  confirmPassword: string
  role?: string
}

export interface UserProfile {
  id: string
  username: string
  role: string
  avatar?: string
  permissions: string[]
  createdAt: string
  lastLoginAt: string
}

export interface AuthResponse {
  token: string
  refreshToken: string
  profile: UserProfile
}

export const AuthService = {
  async login(params: LoginParams): Promise<AuthResponse> {
    return post<AuthResponse>(AuthAPI.login, params)
  },

  async register(params: RegisterParams): Promise<AuthResponse> {
    return post<AuthResponse>(AuthAPI.register, params)
  },

  async logout(): Promise<void> {
    return post(AuthAPI.logout)
  },

  async refreshToken(refreshToken: string): Promise<AuthResponse> {
    return post<AuthResponse>(AuthAPI.refreshToken, { refreshToken })
  },

  async getProfile(): Promise<UserProfile> {
    return get<UserProfile>(AuthAPI.profile)
  },
}
